﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="Gammar.Price.PageControl.js" />
/// <reference path="Gammar.Price.Web.Compare.js" />

Type.registerNamespace("Gammar.Price.Web");

Gammar.Price.Web.PagerPosition = function() {
    /// <field name="both" type="Number" integer="true" static="true"></field>
    /// <field name="top" type="Number" integer="true" static="true"></field>
    /// <field name="bottom" type="Number" integer="true" static="true"></field>
    if (arguments.length !== 0) throw Error.parameterCount();
    throw Error.notImplemented();
}

Gammar.Price.Web.PagerPosition.prototype = {
    both: 0,
    top: 1,
    bottom: 2
}

Gammar.Price.Web.PagerPosition.registerEnum('Gammar.Price.Web.PagerPosition');

Gammar.Price.Web.PriceEntity = function(element) {
    Gammar.Price.Web.PriceEntity.initializeBase(this, [element]);
    this._itemsPanel = null;
    this._topPagerPanel = null;
    this._bottomPagerPanel = null;
    this._pageSize = 20;
    this._pagerCssName = "";
    this._pagerPosition = Gammar.Price.Web.PagerPosition.top;
    this._chkSelectControlName = "chkCompare";
    this._compareUrl = "../Compare/Default.aspx";
}

Gammar.Price.Web.PriceEntity.prototype = {
    initialize: function() {
        Gammar.Price.Web.PriceEntity.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
    },
    
    get_PagerPosition: function() {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._pagerPosition;
    },
    
    set_PagerPosition: function(value) {
        /// <summary />
        /// <param name="value" type="Gammar.Price.Web.PagerPosition" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: Gammar.Price.Web.PagerPosition}
        ]);
        if (e) throw e;        
        this._pagerPosition = value;
    },
    
    get_PagerCssName: function() {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._pagerCssName;
    },
    
    set_PagerCssName: function(value) {
        /// <summary />
        /// <param name="value" type="String" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: String, mayBeNull: true}
        ]);
        if (e) throw e;        
        this._pagerCssName = value;
    },    
    
    _get_ItemsPanel: function() {
        return this._itemsPanel;
    },
    
    _set_ItemsPanel: function(value) {
        this._itemsPanel = value;
    },
    
    _get_topPagerPanel: function() {
        return this._topPagerPanel;
    },
    
    _set_topPagerPanel: function(value) {
        this._topPagerPanel = value;
    },
    
    _get_bottomPagerPanel: function() {
        return this._bottomPagerPanel;
    },
    
    _set_bottomPagerPanel: function(value) {
        this._bottomPagerPanel = value;
    },
    
    get_PageSize: function() {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._pageSize;
    },
    
    set_PageSize: function(value) {
        /// <summary />
        /// <param name="value" type="Number" integer="true" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: Number, integer: true}
        ]);
        if (e) throw e;        
        this._pageSize = value;
    },
    
    _createDynamicLoadingPanel: function() {
        var div = document.createElement("DIV");
        div.innerHTML = "Loading ...";
        Sys.UI.DomElement.toggleCssClass(div, "loading");
        
        var element = this.get_element();
        element.appendChild(div);
        return div;          
    },
    
    _get_guid: function() {
        return this._guid;
    },
    
    _set_guid: function(value) {
        this._guid = value;
    },
        
    updated: function(guid) {
        this._callWebServices(0, this.get_PageSize(), guid);
    },
    
    _callWebServices: function(pageIndex, pageSize, guid) {        
        // 先显示获得数据状态面板。
        var div = this._createDynamicLoadingPanel();
        div.style.display = "block";
        var criteria = "";
        var search = typeof(guid) !== "undefined" ? guid : window.location.search.split("?");
        if (search.length === 2) {
            var paramName = "id";
            var params = search[1].split("&");
            for(var i = 0; i < params.length; i++)
            {
                if (params[i].indexOf(String.format("{0}=", paramName)) > -1)
                {
                    criteria = params[i].split("=")[1];
                    break;
                }
            }
        }
        else if (search.length === 1 && search[0].length === 0) {
            div.style.display = 'none';
            return;
        }
        else {
            criteria = search;
        }
        this._set_guid(criteria);
        div.guid = criteria;
        WebService.RetrieveEntityList(pageIndex, pageSize, criteria,
            Function.createDelegate(this, this._successRetrieveList), 
            Function.createDelegate(this, this._failRetrieveList),
            div);
    },
    
    _successRetrieveList: function (result, context) {
        if (context.guid !== this._get_guid()) return;
        
        if (typeof(result) !== "object") {
            context.innerHTML = "Error occur";
            return;
        }
            
        // Create Top Pager
        if (result.Items.length > 0)
            this._createCompareButton();
        if (result.Items.length > 0 && (this.get_PagerPosition() === Gammar.Price.Web.PagerPosition.both || this.get_PagerPosition() === Gammar.Price.Web.PagerPosition.top)) {
            this._createPager(result, Function.createDelegate(this, this._set_topPagerPanel));
        }
        else {
            this._showNoData();
        }
        
        // Create DataList
        this._populateDataItems(result.Items);
        
        // Create Bottom Pager
        if (result.Items.length > 0)
            this._createCompareButton();
        if (result.Items.length > 0 && (this.get_PagerPosition() === Gammar.Price.Web.PagerPosition.both || this.get_PagerPosition() === Gammar.Price.Web.PagerPosition.bottom))
            this._createPager(result, Function.createDelegate(this, this._set_bottomPagerPanel));        
        context.style.display = 'none';
    },
    
    _createCompareButton: function() {
        var div = document.createElement("DIV");
        var element = this.get_element();
        element.appendChild(div);
        var button = document.createElement("INPUT");
        button.type = "button";
        button.value = "比较";
        div.appendChild(button);
        $create(Gammar.Price.Web.Compare, {CheckboxControlName: this._chkSelectControlName, CompareUrl: this._compareUrl}, null, null, button);
    },
    
    _createPager: function(result, handler) {
        var dataCount = result.DataCount;
        var pageIndex = result.PageIndex;
        var pageSize = this.get_PageSize();
        var element = this.get_element();
        
        var div = document.createElement("DIV");
        div.className = this.get_PagerCssName();
        handler(div);
        element.appendChild(div);
        $create(Gammar.Price.Web.PagerControl, 
            {DataCount: dataCount, PageIndex: pageIndex, PageSize: pageSize}, 
            {pageClick: Function.createDelegate(this, this._pageClick)}, 
            null, div);
    },
    
    _showNoData: function() {
        var element = this.get_element();
        var div = document.createElement("DIV");
        div.innerHTML = "没有数据！";
        element.appendChild(div);        
    },
        
    _pageClick: function(sender, pageClickEventArgs) {
        var element = this.get_element();
        element.innerHTML = "";
        var itemPanel = this._get_ItemsPanel();
        if (itemPanel.parentElement === element) element.removeChild(itemPanel);
        var paperPanel = this._get_topPagerPanel();
        if (paperPanel.parentElement === element) element.removeChild(paperPanel);
        paperPanel = this._get_bottomPagerPanel();
        if (paperPanel.parentElement === element) element.removeChild(paperPanel);
        
        this._callWebServices(pageClickEventArgs.pageIndex, this.get_PageSize(), this._get_guid());
    },
    
    _populateDataItems: function(items) {
        if (items.length > 0) {
            var tb = document.createElement("TABLE");
            this._set_ItemsPanel(tb);
            
            var element = this.get_element();
            element.appendChild(tb);
            
            var _target = "_blank";
            if ((Sys.Browser.InternetExplorer === Sys.Browser.agent && Sys.Browser.version > 6) 
                || (Sys.Browser.agent === Sys.Browser.Firefox)
                || (Sys.Browser.agent === Sys.Browser.Safari)) _target = "_new";
                
            var tr = tb.insertRow(-1);
            tr.className = "th";
            var td = tr.insertCell(-1);
            td.innerHTML = "";
            td = tr.insertCell(-1);
            td.innerHTML = "课程";
            td = tr.insertCell(-1);
            td.innerHTML = "价格";
            td = tr.insertCell(-1);
            td.innerHTML = "培训机构";
            td = tr.insertCell(-1);
            td.innerHTML = "来源";
            
            tr = tb.insertRow(-1);
            td = tr.insertCell(-1);
            td.colSpan = 5;
            td.innerHTML = "<hr />"
            
            for(var i=0;i<items.length;i++) {
                var entity = items[i];
                tr = tb.insertRow(-1);
                td = tr.insertCell(-1);
                td.innerHTML = String.format("<input id=\"Checkbox{0}\" name=\"{2}\" guid=\"{1}\" type=\"checkbox\" />", i, entity.EntityId, this._chkSelectControlName);
                td = tr.insertCell(-1);
                td.innerHTML = String.format("<a href='../TLP/Default.aspx?id={0}&cid={3}' target='{2}' title='{1}'>{4}</a>", entity.EntityId, entity.Title, _target, entity.CategoryId, entity.EntityName);
                td = tr.insertCell(-1);
                td.innerHTML = entity.Price;
                td = tr.insertCell(-1);
                td.innerHTML = entity.PresideUnit;
                td = tr.insertCell(-1);
                td.innerHTML = entity.SrcWeb;
            }
        }
    },
    
    _failRetrieveList: function (result, context) {
        if (context.guid !== this._get_guid()) return;
        context.innerHTML = result.get_message();
    },
    
    dispose: function() {        
        //Add custom dispose actions here
        this._itemsPanel = null;
        this._topPagerPanel = null;
        this._bottomPagerPanel = null;

        Gammar.Price.Web.PriceEntity.callBaseMethod(this, 'dispose');
    }
}
Gammar.Price.Web.PriceEntity.registerClass('Gammar.Price.Web.PriceEntity', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
